package com.hx.hospital.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.hospital.mapper.PositionMapper;
import com.hx.hospital.pojo.Position;
import com.hx.hospital.service.PositionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Position> findAll() {
        return positionMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Position> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Position> positions = (Page<Position>) positionMapper.selectAll();
        return new PageResult<Position>(positions.getTotal(),positions.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Position> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return positionMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Position> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Position> positions = (Page<Position>) positionMapper.selectByExample(example);
        return new PageResult<Position>(positions.getTotal(),positions.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Position findById(Long id) {
        return positionMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param position
     */
    public void add(Position position) {
        positionMapper.insert(position);
    }

    /**
     * 修改
     * @param position
     */
    public void update(Position position) {
        positionMapper.updateByPrimaryKeySelective(position);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        positionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Position.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 职称
            if(searchMap.get("position")!=null && !"".equals(searchMap.get("position"))){
                criteria.andLike("position","%"+searchMap.get("position")+"%");
            }


        }
        return example;
    }

}
